const ajax = require('../utils/ajax.js');
var regeneratorRuntime = require('../utils/runtime.js')
const app = getApp()
var serverUrl = app.serverUrl;//地址
var secretKey = app.secretKey;
var appKey = app.appKey;
var signMethod = app.signMethod;
var version = app.version;
/**
 * 登录
 */
function login(success){
  wx.login({
    success: function (res) {
      console.log("code:" + res.code)
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      ajax.request({
        method: 'POST',
        url: '/app/thirdPart/wxLogin',
        data:{
          code:res.code
        },
        success: loginRes => {
          wx.setStorageSync('token', loginRes.data.data.tokenInfo.token);
          wx.setStorageSync('wxInfo',loginRes.data.data.wxInfo)
          loginRes.data.data.userInfo.userIco = wx.getStorageSync('wxUserInfo').avatarUrl;
          wx.setStorageSync('userInfo', loginRes.data.data.userInfo)
          if (success) {
            success(loginRes);
          }
        }
      })
    }
  })
}
/**
 * 检验sessionKey是否过期
 */
 function checkSessionKey(opt){
  wx.checkSession({
    success: function (res) {
      console.log("处于登录态");
    },
    fail: function (res) {
      console.log("需要重新登录");
      login()
    }
  })
}
/**
 * 获取oss参数
 */
function getOSSData(opt){
  ajax.request({
    method: 'POST',
    url: '/app/sysSetting/getOSSData',
    data: opt.data,
    success: opt.success
  })
}
/**
 * 获取活动列表
 */
function getActivityList(opt){
  ajax.request({
    method: 'POST',
    url: '/app/activity/query',
    data:opt.data,
    success:opt.success
  })
}
/**
 * 获取评论
 */
function getCommentList(opt){
  ajax.request({
    method:'POST',
    url:'/app/activityComment/query',
    data:opt.data,
    success:opt.success
  })
}
/**
 * 发送评论
 */
 async function sendComment(opt){
  if(!wx.getStorageSync('token')){
    wx.showModal({
      title: '温馨提示',
      content: '登录认证后才能发布作品哦!',
      confirmText: '立即登录',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/userLogin/loginHome',
          })
        }
      }
    })
  }else{
    ajax.request({
      method: 'POST',
      url: '/app/activity/comment',
      data: opt.data,
      success: opt.success
    })
  }
}
/**
 * 获取指定评论的回复
 */
function getReplyList(opt){
  ajax.request({
    method: 'POST',
    url: '/app/activityComment/query',
    data: opt.data,
    success: opt.success
  })
}

/**
 * 匿名点赞
 */
function favourNoLogin(opt){
  ajax.request({
    method: 'POST',
    url: '/app/activity/favourNoLogin',
    data:opt.data,
    success:opt.success
  })
}

/**
 * 取消匿名点赞
 */
function cancelFavourNoLogin(opt){
  ajax.request({
    method: 'POST',
    url: '/app/activity/cancelFavourNoLogin',
    data: opt.data,
    success: opt.success
  })
}

/**
 * 登录点赞
 */
function favourLogin(opt){
  ajax.request({
    method: 'POST',
    url: '/app/activity/favour',
    data: opt.data,
    success: opt.success
  })
}
/**
 * 登录取消点赞
 */
function cancelFavourLogin(opt){
  ajax.request({
    method: 'POST',
    url: '/app/activity/cancelFavour',
    data: opt.data,
    success: opt.success
  })
}

/**
 * 发布并保存活动
 */
function saveAndReleaseActivity(opt){
  if (!wx.getStorageSync('token')) {
    wx.showModal({
      title: '温馨提示',
      content: '登录认证后才能发布作品哦!',
      confirmText: '立即登录',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/userLogin/loginHome',
          })
        }
      }
    })
  } else {
    ajax.request({
      method: 'POST',
      url: '/app/activity/saveAndRelease',
      data: opt.data,
      success: opt.success
    })
  }
}

/**
 * 获取可点歌曲列表(setting暂时的)
 */
function getSettingSong(opt){
  ajax.request({
    method: 'POST',
    url: '/app/settingSong/query',
    data: opt.data,
    success: opt.success
  })
}

/**
 * 获取活动点歌列表
 */
function getActivitySongs(opt){
    ajax.request({
      method: 'POST',
      url: '/app/activitySong/query',
      data: opt.data,
      success: opt.success
    })
}

module.exports = {
  login: login,//登录
  getActivityList: getActivityList,//获取活动列表
  getCommentList: getCommentList,//获取评论列表
  getReplyList: getReplyList,//获取指定评论的回复
  favourNoLogin: favourNoLogin,//匿名点赞
  cancelFavourNoLogin: cancelFavourNoLogin,//取消匿名点赞
  favourLogin: favourLogin,//登录点赞
  cancelFavourLogin: cancelFavourLogin,//登录取消点赞
  checkSessionKey, checkSessionKey,//检验sessionKey是否过期
  sendComment, sendComment,//发送评论
  saveAndReleaseActivity, saveAndReleaseActivity,//保存并发布活动
  getOSSData: getOSSData,//获取oss参数
  getActivitySongs: getActivitySongs,//获取活动点歌列表
  getSettingSong: getSettingSong,//获取可点歌曲列表(临时)
}